﻿@namespace BlazorFluentUI.Lists
@inherits FluentUIComponentBase
@typeparam TItem
@using System.Linq
@using Microsoft.AspNetCore.Components

<div class=@($"ms-DetailsList {ClassName}")
     style=@($"{Style}")
     aria-label=@AriaLabel
     @ref="RootElementReference">
    <div role="grid"
         aria-label=@AriaLabel
         aria-readonly=true
         style="display:flex;flex-direction:column;">
        <div role="presentation"
             class="ms-DetailsList-headerWrapper">
            @if (IsHeaderVisible)
            {
                @if (HeaderTemplate != null)
                {
                    @HeaderTemplate
                }
                else
                {
                    <DetailsHeader Columns=@_adjustedColumns
                                      TItem="TItem"
                                      Selection=@Selection
                                      LayoutMode=@LayoutMode

                                      AriaLabel="ariaLabelForListHeader"
                                      AriaLabelForSelectAllCheckbox="ariaLabelForSelectAllCheckbox"
                                      AriaLabelForToggleAllGroup="ariaLabelForSelectionColumn"

                                      OnColumnAutoResized=@OnColumnAutoResized
                                      OnColumnResized=@OnColumnResizedInternal
                                      CheckboxVisibility=@CheckboxVisibility
                                      OnColumnClick=@OnColumnClick
                                      SelectAllVisibility=@selectAllVisibility />
                }
            }
        </div>
        <div role="presentation"
             class="ms-DetailsList-contentWrapper"
             >
            <FocusZone ClassName="ms-DetailsList-focusZone"
                          Direction="FocusZoneDirection.Vertical"
                          InnerZoneKeystrokeTriggers="new System.Collections.Generic.List<ConsoleKey> { ConsoleKey.RightArrow }"
                          >
                @if (!DisableSelectionZone)
                {
                    <SelectionZone Selection=@Selection
                                      @ref="selectionZone"
                                      DisableRenderOnSelectionChanged="true"
                                      SelectionPreservedOnEmptyClick=@SelectionPreservedOnEmptyClick
                                      SelectionMode=@SelectionMode
                                      EnterModalOnTouch=@EnterModalSelectionOnTouch
                                      OnItemInvoked=@((item,index) => this.OnItemInvoked.InvokeAsync(item))
                                      >
                        @if (GroupBy == null)
                        {
                            <List ItemsSource=@items
                                     TItem="TItem"
                                     IsVirtualizing=@IsVirtualizing
                                     >
                                <ItemTemplate>
                                    @if (RowTemplate != null)
                                    {
                                        RowTemplate(context);
                                    }
                                    else
                                    {
                                        <DetailsRow Item=@context.Item
                                                       GetKey=@(GetKey!)
                                                       ItemIndex=@context.Index
                                                       Columns=@_adjustedColumns
                                                       TItem="TItem"
                                                       OnRowDidMount=@OnRowDidMountInternal
                                                       OnRowWillUnmount=@OnRowWillUnmountInternal
                                                       SelectionMode=@SelectionMode
                                                       Selection=@Selection
                                                       Compact=@Compact
                                                       CheckboxVisibility=@CheckboxVisibility />
                                    }
                                </ItemTemplate>
                            </List>
                        }
                        else
                        {
                            <GroupedListAuto ItemsSource=@items
                                            @ref=@groupedList
                                            TItem="TItem"
                                            TKey="object"
                                            GetKey=@(GetKey!)
                                            GroupBy=@GroupBy
                                            SortBy=@groupSortSelectors
                                            SortDescending=@groupSortDescendingList
                                            IsVirtualizing=@IsVirtualizing
                                                Selection=@Selection
                                            SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)>
                                <ItemTemplate>
                                    <DetailsRow TItem="TItem"
                                                   Item=@(((PlainItem3<TItem,object>)context.Item!).Item)
                                                   GetKey=@GetKey
                                                   ItemIndex=@context.Index
                                                   Selection=@Selection
                                                   OnRowDidMount=@OnRowDidMountInternal
                                                   OnRowWillUnmount=@OnRowWillUnmountInternal
                                                   Columns=@_adjustedColumns
                                                   SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)
                                                   CheckboxVisibility=@CheckboxVisibility
                                                   Compact=@Compact />
                                </ItemTemplate>
                            </GroupedListAuto>
                        }
                    </SelectionZone>

                }
                else
                {
                    @if (GroupBy == null)
                    {
                        <List ItemsSource=@items
                                 TItem="TItem"
                                 >
                            <ItemTemplate>
                                @if (RowTemplate != null)
                                {
                                    RowTemplate(context);
                                }
                                else
                                {
                                    <DetailsRow Item=@context.Item
                                                   ItemIndex=@context.Index
                                                   TItem="TItem"
                                                   Columns=@_adjustedColumns
                                                   SelectionMode=@SelectionMode
                                                   Selection=@Selection
                                                   Compact=@Compact
                                                   CheckboxVisibility=@CheckboxVisibility />
                                }
                            </ItemTemplate>
                        </List>
                    }
                    else
                    {
                        <GroupedListAuto ItemsSource=@items
                                            @ref=@groupedList
                                            TItem="TItem"
                                            TKey="object"
                                            GetKey=@(GetKey!)
                                            GroupBy=@GroupBy
                                            SortBy=@groupSortSelectors
                                            SortDescending=@groupSortDescendingList
                                            IsVirtualizing=@IsVirtualizing
                                            Selection=@Selection
                                            SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)>
                            <ItemTemplate>
                                <DetailsRow TItem="TItem"
                                               Item=@(((PlainItem3<TItem,object>)context.Item!).Item)
                                               GetKey=@GetKey
                                               ItemIndex=@context.Index
                                               Selection=@Selection
                                               Columns=@_adjustedColumns
                                               SelectionMode=@(CheckboxVisibility != CheckboxVisibility.Hidden ? SelectionMode : SelectionMode.None)
                                               CheckboxVisibility=@CheckboxVisibility
                                               Compact=@Compact />
                            </ItemTemplate>
                        </GroupedListAuto>
                    }
                }
            </FocusZone>
        </div>
        @if (FooterTemplate != null)
        {
            @FooterTemplate
        }
    </div>
</div>
